package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiYNEnum;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;

@Cmd(code = Protocol.C2_SON_HAI_ENTER_ACTIVITY, desc = "山海伏兽-进入活动")
public class CrossSonHaiEnterActivityCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiCrossEnterActivityReqMsg reqMsg = SonHaiProto.SonHaiCrossEnterActivityReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiEnterActivityRespMsg.Builder respMsg = SonHaiProto.SonHaiEnterActivityRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_ENTER_ACTIVITY, respMsg));
            return;
        }
        //进入活动
        try {
            //存储userBase
            if(reqMsg.hasUserInfo()){
                UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
                CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
            }
            //进入活动
            activity.enterActivity(packet.getPlayerId(), serverId, reqMsg.getPower(), reqMsg.getTalentAddition(), reqMsg.getLanguage());
            CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
            CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
            //构造PB
            respMsg.setRet(0);
            SonHaiProto.SonHaiUserData.Builder userBuilder = SonHaiActivityPb.buildSonHaiUserData(user, team, true);
            respMsg.setUserData(userBuilder);
            respMsg.setSelectRegionFlag(user.getSelectRegionFlag() == SonHaiYNEnum.YES.getType());
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_ENTER_ACTIVITY, respMsg));
    }

    @Override
    public void sendData() {

    }
}
